package com.example.administrator.caigou51.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;


import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.activity.TradeSuccessActivity;
import com.example.administrator.caigou51.bean.WeXinPaySuccess;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.Constant;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.umeng.analytics.MobclickAgent;

import de.greenrobot.event.EventBus;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    public static String OrderId = "";
    public static String OrderUrl = "";
    public static String WXPayFlag;
    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, Constant.SystemContext.WXID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        //Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

            if (resp.errCode == 0) {
                App.showToast("支付成功");
                EventBus.getDefault().post(new WeXinPaySuccess());

            } else {
              //  App.showToast(JSON.toJSONString(resp));
              //  App.showToast("支付失败");
            }

        }

        finish();
    }
}